from datetime import datetime

import requests
from lxml import etree
import re
import os
import pdfkit


# 参考https://www.52pojie.cn/thread-990598-1-1.html

# 总结： 刚刚开始的时候，图片没有 下载下来，是因为生成的html文件中，图片的url不完整，所以图片就没有下载下来
# 首先，先通过xpath 定位到你想要保存的内容的路径，例如下面的ehtml.xpath('//div[@id="cnblogs_post_body"]')[0]  因为这里是一个list，所以要
# 用下标获取第一个元素，否则python 报错

def gethtml(url, encode):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
    }
    r = requests.get(url, headers=headers)
    r.encoding = encode
    return r.text


def writehtml(path, str):
    f = open(path, 'w+', encoding='utf-8')
    f.write(str)
    f.close


def validateTitle(title):
    rstr = r"[\/\\\:\*\?\"\<\>\|]"
    new_title = re.sub(rstr, "_", title)
    return new_title


def mkdir(path):
    path = path.strip()
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)
    return True


def getdata(url, pdf, folder):
    print('开始获取,请稍候...')
    if mkdir(savepath + folder):
        html = gethtml(url, 'utf-8')
        ehtml = etree.HTML(html)
        # //*[@id="article_content"]
        strs = ehtml.xpath('//*[@id="article_content"]')[0]  # 这个id基本不用改变，试过下载了几篇文章，都是同一个id
        txt = etree.tostring(strs, encoding="utf-8", pretty_print=True, method="html").decode("utf-8")

        # txtl2 = re.sub('<h[2,4]>', '<h3>', txtl2)
        # txtl2 = re.sub('</h[2,4]>', '</h3>', txtl2)

        # 这一步是下载图片的
        txtl2 = re.sub('src="//', 'src="https://', txt)
        # txtl2 = '<h2>' + fname + '</h2>' + txtl2
        # txt = txt + txtl2
        writehtml(savepath + folder + '\\' + folder + '.html', txtl2)
        #  先转html，然后再转pdf
        if pdf:
            print('开始生成pdf,请稍候...')
            path_wk = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'  # wkhtmltopdf安装位置
            config = pdfkit.configuration(wkhtmltopdf=path_wk)
            options = {
                'page-size': 'A4',
                'margin-top': '0.75in',
                'margin-right': '0.75in',
                'margin-bottom': '0.75in',
                'margin-left': '0.75in',
                'encoding': "UTF-8",
                'outline': None
            }
            pdfkit.from_file([savepath + folder + '\\' + folder + '.html'], savepath + folder + '\\' + folder + '.pdf',
                             options=options, configuration=config)
    print('任务完成!')


if __name__ == '__main__':
    url = 'https://blog.csdn.net/qq1404510094/article/details/80577555/'  # 获取教程url地址
    folder = 'java注解-最通俗易懂的讲解' + datetime.strftime(datetime.now(), '%Y-%m-%d-%H-%M-%S')
    savepath = 'D:\\csdn\\'  # 保存位置

    getdata(url, True, folder)  # 后面True表示生成PDF，False不生成

    print("保存路径： " + savepath + folder + '\\' + folder + '.pdf')
    os.startfile(savepath + folder )
